package repository

import (
	"oa-system/internal/model"
	"oa-system/pkg/errors"

	"gorm.io/gorm"
)

type DictionaryRepository interface {
	GetDictionariesByType(dictType string) ([]model.Dictionary, error)
	CreateDictionary(dict *model.Dictionary) (*model.Dictionary, error)
	UpdateDictionary(dict *model.Dictionary) (*model.Dictionary, error)
	DeleteDictionary(id uint) error
	GetDictionaryByID(id uint) (*model.Dictionary, error)
	GetAllDictionaries(page, pageSize int, typeSearch, labelSearch string) ([]model.Dictionary, int64, error)
	GetDictionaryByTypeAndValue(dictType, value string) (*model.Dictionary, error)
}

type dictionaryRepository struct {
	db *gorm.DB
}

func NewDictionaryRepository(db *gorm.DB) DictionaryRepository {
	return &dictionaryRepository{db: db}
}

func (r *dictionaryRepository) GetDictionariesByType(dictType string) ([]model.Dictionary, error) {
	var dictionaries []model.Dictionary
	if err := r.db.Where("type = ?", dictType).
		Order("sort ASC, id DESC").
		Find(&dictionaries).Error; err != nil {
		return nil, errors.NewInternalServerError("获取字典列表失败", err)
	}
	return dictionaries, nil
}

func (r *dictionaryRepository) CreateDictionary(dict *model.Dictionary) (*model.Dictionary, error) {
	if err := r.db.Create(dict).Error; err != nil {
		return nil, errors.NewInternalServerError("创建字典失败", err)
	}
	return dict, nil
}

func (r *dictionaryRepository) UpdateDictionary(dict *model.Dictionary) (*model.Dictionary, error) {
	if err := r.db.Model(dict).Omit("created_at").Updates(dict).Error; err != nil {
		return nil, errors.NewInternalServerError("更新字典失败", err)
	}
	return dict, nil
}

func (r *dictionaryRepository) DeleteDictionary(id uint) error {
	if err := r.db.Delete(&model.Dictionary{}, id).Error; err != nil {
		return errors.NewInternalServerError("删除字典失败", err)
	}
	return nil
}

func (r *dictionaryRepository) GetDictionaryByID(id uint) (*model.Dictionary, error) {
	var dict model.Dictionary
	if err := r.db.First(&dict, id).Error; err != nil {
		return nil, errors.NewNotFoundError("字典不存在", err)
	}
	return &dict, nil
}

func (r *dictionaryRepository) GetAllDictionaries(page, pageSize int, typeSearch, labelSearch string) ([]model.Dictionary, int64, error) {
	var dictionaries []model.Dictionary
	var total int64

	query := r.db.Model(&model.Dictionary{})

	if typeSearch != "" {
		query = query.Where("type LIKE ?", "%"+typeSearch+"%")
	}
	if labelSearch != "" {
		query = query.Where("label LIKE ?", "%"+labelSearch+"%")
	}

	if err := query.Count(&total).Error; err != nil {
		return nil, 0, errors.NewInternalServerError("获取字典总数失败", err)
	}

	offset := (page - 1) * pageSize
	if err := query.Offset(offset).Limit(pageSize).Find(&dictionaries).Error; err != nil {
		return nil, 0, errors.NewInternalServerError("获取字典列表失败", err)
	}

	return dictionaries, total, nil
}

func (r *dictionaryRepository) GetDictionaryByTypeAndValue(dictType, value string) (*model.Dictionary, error) {
	var dict model.Dictionary
	if err := r.db.Where("type = ? AND value = ?", dictType, value).First(&dict).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, nil
		}
		return nil, errors.NewInternalServerError("获取字典失败", err)
	}
	return &dict, nil
}
